{% extends 'backend.html' %}
{% block backend_main %}
    <div class="panel panel-info">
        <div class="panel-heading">
            <h3 class="panel-title">添加分类</h3>
        </div>
        <div class="panel-body">
            <form class="layui-form layui-form-pane" id="add_category_form">
                {% csrf_token %}
                <div class="layui-form-item">
                    {% if condition == 'category' %}
                        <label for="category" class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="category" autocomplete="off" placeholder="请输入"
                                   lay-verify="required" class="layui-input" id="title">
                        </div>
                    {% elif condition == 'tag' %}
                        <label for="tag" class="layui-form-label">标签名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="tag" autocomplete="off" placeholder="请输入"
                                   lay-verify="required" class="layui-input" id="title">
                        </div>
                    {% endif %}
                </div>
                <div class="layui-form-item">
                    <label for="phone" class="layui-form-label">个人站点</label>
                    <div class="layui-input-block">
                        <input type="text" name="blog" autocomplete="off" placeholder="请输入"
                               lay-verify="required" class="layui-input" value="{{ request.user.blog.name }}" disabled>
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn" id="btn_submit_adv" lay-submit lay-filter="btn_submit_category">确认
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </form>
        </div>
    </div>
    <script>
    $(document).ready(
        layui.use(['form'], function () {
            let form = layui.form;
            // 提交事件
            form.on('submit(btn_submit_category)', function (data) {
                let field = data.field;
                $.ajax({
                    url: '{% url 'backend:addCategoryTag' condition %}',
                    type: 'post',
                    data: field,
                    success: function (response) {
                        Swal.fire({
                            title: response.msg,
                            icon: "success",
                            confirmButton: true
                        }).then((result) => {
                            if (result.isConfirmed) {
                                window.location.href =
                                {% url 'backend:backend' %}
                            }
                        })
                    },
                })
                return false
            });
        })
    )
</script>
{% endblock %}
